#include <bits/stdc++.h>
// 2024/12/14
// tag: 
using namespace std;

int x, y, k, m, ans;
bool vis[101][101];

signed main()
{
    cin >> x >> y >> k >> m;
    queue<pair<int, int>> q;
    q.push({0, 0}), vis[0][0] = 1, ans = m;
    while (k-- && !q.empty()){
        int len = q.size();
        while (len--){
            auto p = q.front();
            q.pop();
            int a = p.first, b = p.second;
            if (!vis[x][b])
                vis[x][b] = true, q.push({x, b}), ans = min(ans, abs(m - x - b));
            if (!vis[a][y])
                vis[a][y] = true, q.push({a, y}), ans = min(ans, abs(m - a - y));
            if (!vis[0][b])
                vis[0][b] = true, q.push({0, b}), ans = min(ans, abs(m - b));
            if (!vis[a][0])
                vis[a][0] = true, q.push({a, 0}), ans = min(ans, abs(m - a));
            if (!vis[min(x, a + b)][a + b - min(x, a + b)])
                vis[min(x, a + b)][a + b - min(x, a + b)] = true, q.push({min(x, a + b), a + b - min(x, a + b)}), ans = min(ans, abs(m - a - b));
            if (!vis[a + b - min(y, b + a)][min(y, b + a)])
                vis[a + b - min(y, b + a)][min(y, b + a)] = true, q.push({a + b - min(y, b + a), min(y, b + a)}), ans = min(ans, abs(m - a - b));
        }
    }
    cout << ans << endl;
    return 0;
}
